package cn.fz.ssedemo.handler;

import cn.fz.ssedemo.utils.ContextUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    // 创建时添加
    @Override
    public void insertFill(MetaObject metaObject) {
        // 生成一个1000-9999的随机整数
        // this.setFieldValByName("createdId", (int) (Math.random() * 9000 + 1000), metaObject);
        this.setFieldValByName("createdId", ContextUtils.getCurrentId(), metaObject);
        this.setFieldValByName("createTime", new Date(), metaObject);
    }

    // 更新时添加
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updatedId", ContextUtils.getCurrentId(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
